/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.wallet.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.wallet.constant.NotificationEnum;
import com.pig4cloud.pigx.wallet.entity.Notification;
import com.pig4cloud.pigx.wallet.service.NotificationService;
import com.pig4cloud.pigx.wallet.vo.MobileNotificationVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户消息通知
 *
 * @date 2022-10-21 16:07:37
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/notification")
@Api(value = "notification", tags = "用户消息通知管理")
public class NotificationController {

	private final NotificationService notificationService;

	/**
	 * 分页查询
	 *
	 * @param page         分页对象
	 * @param notification 用户消息通知
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('wallet_notification_view')")
	public R getNotificationPage(Page page, Notification notification) {
		return R.ok(notificationService.page(page, Wrappers.query(notification)));
	}


	/**
	 * 通过id查询用户消息通知
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('wallet_notification_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(notificationService.getById(id));
	}

	/**
	 * 新增用户消息通知
	 *
	 * @param notification 用户消息通知
	 * @return R
	 */
	@ApiOperation(value = "新增用户消息通知", notes = "新增用户消息通知")
	@SysLog("新增用户消息通知")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('wallet_notification_add')")
	public R save(@RequestBody Notification notification) {
		notification.setId(null);
		return R.ok(notificationService.save(notification));
	}

	/**
	 * 修改用户消息通知
	 *
	 * @param notification 用户消息通知
	 * @return R
	 */
	@ApiOperation(value = "修改用户消息通知", notes = "修改用户消息通知")
	@SysLog("修改用户消息通知")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('wallet_notification_edit')")
	public R updateById(@RequestBody Notification notification) {
		return R.ok(notificationService.updateById(notification));
	}

	/**
	 * 通过id删除用户消息通知
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除用户消息通知", notes = "通过id删除用户消息通知")
	@SysLog("通过id删除用户消息通知")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('wallet_notification_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(notificationService.removeById(id));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param notification 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('wallet_notification_export')")
	public List<Notification> export(Notification notification) {
		return notificationService.list(Wrappers.query(notification));
	}

	/**
	 * Dapp用户分页查询
	 *
	 * @param page 分页对象
	 * @return
	 */
	@ApiOperation(value = "Dapp用户分页查询", notes = "Dapp用户分页查询")
	@PostMapping("/app/page")
	public R<Page<List<MobileNotificationVO>>> getNotificationPageByUserId(@RequestBody Page page) {
		// 用户通知已读
		Notification updateNotification = new Notification();
		updateNotification.setIsRead(NotificationEnum.READ.getCode());
		notificationService.update(updateNotification, new LambdaQueryWrapper<Notification>()
				.eq(Notification::getUserId, SecurityUtils.getUser().getId()));

		Page<List<MobileNotificationVO>> mobileNotificationPage = notificationService.page(page, new LambdaQueryWrapper<Notification>()
				.eq(Notification::getUserId, SecurityUtils.getUser().getId())
				.orderByDesc(Notification::getCreateTime));
		return R.ok(mobileNotificationPage);
	}

	/**
	 * Dapp用户未读消息通知数量
	 *
	 * @return
	 */
	@ApiOperation(value = "Dapp用户未读消息通知数量", notes = "Dapp用户未读消息通知数量")
	@GetMapping("/app/userToUnreadNumber")
	public R userToUnreadNumber() {
		return R.ok(notificationService.count(new LambdaQueryWrapper<Notification>()
				.eq(Notification::getUserId, SecurityUtils.getUser().getId())
				.eq(Notification::getIsRead, NotificationEnum.UNREAD.getCode())));
	}

	/**
	 * Dapp用户消息一键已读
	 *
	 * @return
	 */
	@ApiOperation(value = "Dapp用户消息一键已读", notes = "Dapp用户消息一键已读")
	@GetMapping("/app/userToRead")
	public R userToRead() {

		// 设置已读
		Notification updateNotification = new Notification();
		updateNotification.setIsRead(NotificationEnum.READ.getCode());

		return R.ok(notificationService.update(updateNotification, new LambdaQueryWrapper<Notification>()
				.eq(Notification::getUserId, SecurityUtils.getUser().getId())));
	}

	/**
	 * Dapp用户消息一键删除
	 *
	 * @return
	 */
	@ApiOperation(value = "Dapp用户消息一键删除", notes = "Dapp用户消息一键删除")
	@GetMapping("/app/userNotificationDelete")
	public R userNotificationDelete() {
		return R.ok(notificationService.remove(new LambdaQueryWrapper<Notification>()
				.eq(Notification::getUserId, SecurityUtils.getUser().getId())));
	}

}
